#include "MoveCommand.h"

#include <QtGui/QGraphicsItem>

MoveCommand::MoveCommand(QList<QGraphicsItem *> movingItems, QList<QPointF> oldPos)
{
    this->movingItems = movingItems;
    this->oldPos = oldPos;
    this->isRedo = false;

    foreach (QGraphicsItem *item, movingItems) {
        newPos.push_back(item->pos());
    }

    if (movingItems.count() <= 1)
        this->setText(QString("Item Moved From [%1, %2] To [%3, %4]").arg(oldPos.first().x())
                                                                     .arg(oldPos.first().y())
                                                                     .arg(newPos.first().x())
                                                                     .arg(newPos.first().y()));
    else
        this->setText(QString("%1 Items Moved").arg(movingItems.count()));
}

void MoveCommand::undo()
{
    this->isRedo = true;

    for (int i = 0; i < this->movingItems.count(); i++) {
        this->movingItems[i]->setPos(oldPos[i]);
    }
}

void MoveCommand::redo()
{
    if (this->isRedo == true) {
        for (int i = 0; i < this->movingItems.count(); i++) {
            this->movingItems[i]->setPos(newPos[i]);
        }
    }
}
